package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import com.ruoyi.system.domain.Company;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EasyMapper;
import com.ruoyi.system.domain.Easy;
import com.ruoyi.system.service.IEasyService;

/**
 * 易告知Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-04
 */
@Service
public class EasyServiceImpl implements IEasyService
{
    @Autowired
    private EasyMapper easyMapper;

    @Autowired
    private CompanyServiceImpl companyService;
    @Autowired
    private ISysJobService sysJobService;
    /**
     * 查询易告知
     * 
     * @param easyId 易告知主键
     * @return 易告知
     */
    @Override
    public Easy selectEasyByEasyId(Long easyId)
    {
        return easyMapper.selectEasyByEasyId(easyId);
    }

    /**
     * 查询易告知列表
     * 
     * @param easy 易告知
     * @return 易告知
     */
    @Override
    public List<Easy> selectEasyList(Easy easy)
    {

        return easyMapper.selectEasyList(easy);
    }

    /**
     * 新增易告知
     *
     * @param easy 易告知
     * @return 结果
     */
    @Override
    public void insertEasy(Easy easy)
    {
        easy.setFakeDelete(1l);
        easyMapper.insertEasy(easy);
        Company company = companyService.selectCompanyByCompanyId(easy.getCompanyId());
        Long easyId = easy.getEasyId();
        SysJob sysJob = new SysJob();
        sysJob.setJobEasyId(easyId);
        sysJob.setJobName("向"+company.getCompanyName()+"发送定时邮件");
        sysJob.setJobGroup("DEFAULT");
        sysJob.setInvokeTarget("Task.Email("+"'"+easy.getEmailTheme()+"','"+easy.getEmailContent()+"','"+company.getEmail()+"'"+")");
        sysJob.setCronExpression(easy.getSendInterval());
        sysJob.setMisfirePolicy("3");
        sysJob.setConcurrent("1");
        try {
            sysJobService.insertJob(sysJob);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        } catch (TaskException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改易告知
     * 
     * @param easy 易告知
     * @return 结果
     */
    @Override
    public int updateEasy(Easy easy)
    {
        Company company = companyService.selectCompanyByCompanyId(easy.getCompanyId());
        SysJob sysJob = sysJobService.selectJobByEasyId(easy.getEasyId());
        System.out.println(sysJob);
        if (sysJob!=null){
            System.out.println(sysJob);
            sysJob.setJobName("向"+company.getCompanyName()+"发送定时邮件");
            sysJob.setInvokeTarget("Task.Email("+"'"+easy.getEmailTheme()+"','"+easy.getEmailContent()+"','"+company.getEmail()+"'"+")");
            sysJob.setCronExpression(easy.getSendInterval());
            try {
                sysJobService.updateJob(sysJob);
            } catch (SchedulerException e) {
                throw new RuntimeException(e);
            } catch (TaskException e) {
                throw new RuntimeException(e);
            }
        }
        return easyMapper.updateEasy(easy);
    }

    /**
     * 批量删除易告知
     * 
     * @param easyIds 需要删除的易告知主键
     * @return 结果
     */
    @Override
    public int deleteEasyByEasyIds(Long[] easyIds)
    {
        return easyMapper.deleteEasyByEasyIds(easyIds);
    }

    /**
     * 删除易告知信息
     * 
     * @param easyId 易告知主键
     * @return 结果
     */
    @Override
    public int deleteEasyByEasyId(Long easyId)
    {
        return easyMapper.deleteEasyByEasyId(easyId);
    }
}
